﻿namespace WG3000_COMM.Core
{
    using System;
    using System.Collections;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    using WG3000_COMM.Common;

    public class wgUdpServerCom : IDisposable
    {
        private static ArrayList arrControllerInfo = new ArrayList();
        private ArrayList arrEndPoint;
        private ArrayList arrIGet;
        private ArrayList arrIPPORT;
        private static ArrayList arrSNReceived = new ArrayList();
        private ArrayList arrXIDEndPointMobile;
        private ArrayList arrXIDReceivedMobile;
        private ArrayList arrXIDSend;
        private bool bIncludeIP;
        private static int bUDPCloud = 1;
        private bool bUDPListenStop;
        private bool bWatching;
        private byte[] cmdtemp;
        private const int COMM_UDP_PORT = 0xea60;
        private Thread DealRuninfoPacketThread;
        private long dtXIDSendLast;
        private IPEndPoint endp4broadcast;
        private static long m_send_xid = 0x70000000L;
        private System.Collections.Queue receviedCommandReply;
        private System.Collections.Queue receviedRecords;
        private System.Collections.Queue RemoteEndPointQueue;
        private System.Collections.Queue RemoteEndPointQueue4Mobile;
        private System.Collections.Queue RemoteEndPointQueue4RemoteOpenDoor;
        private static long sequenceId4SendCommand = 0x40000000L;
        private string serverIP;
        private int serverPort;
        private Thread UDPListenThread;
        private System.Collections.Queue UDPQueue;
        private System.Collections.Queue UDPQueue4Mobile;
        private System.Collections.Queue UDPQueue4RemoteOpenDoor;
        private Socket UdpSocket;

        public wgUdpServerCom()
        {
            this.UDPQueue = new System.Collections.Queue();
            this.UDPQueue4Mobile = new System.Collections.Queue();
            this.UDPQueue4RemoteOpenDoor = new System.Collections.Queue();
            this.RemoteEndPointQueue = new System.Collections.Queue();
            this.RemoteEndPointQueue4Mobile = new System.Collections.Queue();
            this.RemoteEndPointQueue4RemoteOpenDoor = new System.Collections.Queue();
            this.cmdtemp = new byte[0x41c];
            this.receviedRecords = new System.Collections.Queue();
            this.arrXIDSend = new ArrayList();
            this.arrXIDReceivedMobile = new ArrayList();
            this.arrXIDEndPointMobile = new ArrayList();
            this.dtXIDSendLast = DateTime.Now.Ticks;
            this.receviedCommandReply = new System.Collections.Queue();
            this.arrIPPORT = new ArrayList();
            this.arrEndPoint = new ArrayList();
            this.arrIGet = new ArrayList();
            try
            {
                this.UdpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                this.SockSpecialSet(ref this.UdpSocket);
                this.UdpSocket.EnableBroadcast = true;
                this.UdpSocket.ReceiveBufferSize = 0x1000000;
                IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 0);
                this.UdpSocket.Bind(localEP);
                this.UDPListenThread = new Thread(new ThreadStart(this.UDPListenProc));
                this.UDPListenThread.Name = "wgUdpServer";
                this.UDPListenThread.IsBackground = true;
                this.UDPListenThread.Start();
                this.DealRuninfoPacketThread = new Thread(new ThreadStart(this.DealRuninfoPacketProc));
                this.DealRuninfoPacketThread.Name = "Deal Run InfoPacket";
                this.DealRuninfoPacketThread.IsBackground = true;
                this.DealRuninfoPacketThread.Start();
                this.DealRuninfoPacketThread = new Thread(new ThreadStart(this.DealRuninfoPacketProc4Mobile));
                this.DealRuninfoPacketThread.Name = "Deal Run InfoPacket Mobile";
                this.DealRuninfoPacketThread.IsBackground = true;
                this.DealRuninfoPacketThread.Start();
                this.DealRuninfoPacketThread = new Thread(new ThreadStart(this.DealRuninfoPacketProc4RemoteOpenDoor));
                this.DealRuninfoPacketThread.Name = "Deal Run InfoPacket RemoteOpenDoor";
                this.DealRuninfoPacketThread.IsBackground = true;
                this.DealRuninfoPacketThread.Start();
                Thread.Sleep(10);
            }
            catch (Exception)
            {
            }
        }

        public wgUdpServerCom(string IP, int port)
        {
            this.UDPQueue = new System.Collections.Queue();
            this.UDPQueue4Mobile = new System.Collections.Queue();
            this.UDPQueue4RemoteOpenDoor = new System.Collections.Queue();
            this.RemoteEndPointQueue = new System.Collections.Queue();
            this.RemoteEndPointQueue4Mobile = new System.Collections.Queue();
            this.RemoteEndPointQueue4RemoteOpenDoor = new System.Collections.Queue();
            this.cmdtemp = new byte[0x41c];
            this.receviedRecords = new System.Collections.Queue();
            this.arrXIDSend = new ArrayList();
            this.arrXIDReceivedMobile = new ArrayList();
            this.arrXIDEndPointMobile = new ArrayList();
            this.dtXIDSendLast = DateTime.Now.Ticks;
            this.receviedCommandReply = new System.Collections.Queue();
            this.arrIPPORT = new ArrayList();
            this.arrEndPoint = new ArrayList();
            this.arrIGet = new ArrayList();
            try
            {
                this.UdpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                this.SockSpecialSet(ref this.UdpSocket);
                this.UdpSocket.EnableBroadcast = true;
                IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(IP), port);
                this.serverIP = IP;
                this.serverPort = port;
                this.UdpSocket.Bind(localEP);
                this.UdpSocket.ReceiveBufferSize = 0x1000000;
                this.UDPListenThread = new Thread(new ThreadStart(this.UDPListenProc));
                this.UDPListenThread.Name = "wgUdpServer";
                this.UDPListenThread.IsBackground = true;
                this.UDPListenThread.Start();
                this.DealRuninfoPacketThread = new Thread(new ThreadStart(this.DealRuninfoPacketProc));
                this.DealRuninfoPacketThread.Name = "Deal Run InfoPacket";
                this.DealRuninfoPacketThread.IsBackground = true;
                this.DealRuninfoPacketThread.Start();
                this.DealRuninfoPacketThread = new Thread(new ThreadStart(this.DealRuninfoPacketProc4Mobile));
                this.DealRuninfoPacketThread.Name = "Deal Run InfoPacket Mobile";
                this.DealRuninfoPacketThread.IsBackground = true;
                this.DealRuninfoPacketThread.Start();
                this.DealRuninfoPacketThread = new Thread(new ThreadStart(this.DealRuninfoPacketProc4RemoteOpenDoor));
                this.DealRuninfoPacketThread.Name = "Deal Run InfoPacket RemoteOpenDoor";
                this.DealRuninfoPacketThread.IsBackground = true;
                this.DealRuninfoPacketThread.Start();
                Thread.Sleep(10);
                this.bWatching = true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        private long byteToLong(byte[] buff, int start, int len)
        {
            long num = 0L;
            for (int i = 0; (i < len) && (i < 4); i++)
            {
                long num3 = buff[i + start];
                num += num3 << (8 * i);
            }
            return num;
        }

        public bool Close()
        {
            try
            {
                this.bUDPListenStop = true;
                Thread.Sleep(20);
                if (this.UDPListenThread != null)
                {
                    this.UDPListenThread.Abort();
                }
                if (this.UDPQueue != null)
                {
                    this.UDPQueue.Clear();
                    this.UDPQueue = null;
                }
                if (this.DealRuninfoPacketThread != null)
                {
                    this.DealRuninfoPacketThread.Abort();
                }
                if (this.UdpSocket != null)
                {
                    this.UdpSocket.Close();
                }
            }
            catch
            {
            }
            return true;
        }

        private void DealRuninfoPacketProc()
        {
            try
            {
                do
                {
                    if (this.UDPQueue.Count > 0)
                    {
                        byte[] buffer;
                        lock (this.UDPQueue.SyncRoot)
                        {
                            buffer = (byte[]) this.UDPQueue.Dequeue();
                        }
                        if (buffer.Length >= 0x40)
                        {
                            if ((buffer.Length % 0x40) == 0)
                            {
                                lock (this.receviedRecords.SyncRoot)
                                {
                                    this.receviedRecords.Enqueue(buffer);
                                }
                            }
                            if ((buffer.Length % 0x40) == 4)
                            {
                                lock (this.receviedRecords.SyncRoot)
                                {
                                    this.receviedRecords.Enqueue(buffer);
                                }
                            }
                            if ((buffer[1] != 0x20) && (buffer[1] != 0x22))
                            {
                                byte[] buffer2 = buffer;
                                lock (this.receviedCommandReply.SyncRoot)
                                {
                                    if (this.receviedCommandReply.Count > 0x2710)
                                    {
                                        this.receviedCommandReply.Clear();
                                    }
                                    this.receviedCommandReply.Enqueue(buffer2);
                                }
                            }
                        }
                        Thread.Sleep(1);
                    }
                    else
                    {
                        Thread.Sleep(10);
                    }
                }
                while (!this.bUDPListenStop);
            }
            catch (Exception)
            {
            }
        }

        private void DealRuninfoPacketProc4Mobile()
        {
            try
            {
                do
                {
                    if (this.UDPQueue4Mobile.Count > 0)
                    {
                        byte[] buffer;
                        IPEndPoint point;
                        lock (this.UDPQueue4Mobile.SyncRoot)
                        {
                            buffer = (byte[]) this.UDPQueue4Mobile.Dequeue();
                            point = (IPEndPoint) this.RemoteEndPointQueue4Mobile.Dequeue();
                        }
                        if ((buffer.Length == 0x40) && (buffer[0] == 0x37))
                        {
                            uint num = BitConverter.ToUInt32(buffer, 4);
                            if (arrSNReceived.IndexOf(num) >= 0)
                            {
                                buffer[0] = 0x17;
                                long num2 = this.getXidOfCommandShort(buffer);
                                long num3 = this.getXid();
                                Array.Copy(BitConverter.GetBytes(num3), 0, buffer, 40, 4);
                                lock (this.arrXIDSend.SyncRoot)
                                {
                                    this.arrXIDSend.Add(num3);
                                    this.arrXIDReceivedMobile.Add(num2);
                                    this.arrXIDEndPointMobile.Add(point);
                                }
                                this.dtXIDSendLast = DateTime.Now.Ticks + 0x47868c00L;
                                this.UDP_OnlySend(buffer, 0L);
                            }
                        }
                    }
                    else
                    {
                        if ((this.arrXIDSend.Count > 0) && (this.dtXIDSendLast < DateTime.Now.Ticks))
                        {
                            lock (this.arrXIDSend.SyncRoot)
                            {
                                this.arrXIDSend.Clear();
                                this.arrXIDReceivedMobile.Clear();
                                this.arrXIDEndPointMobile.Clear();
                            }
                        }
                        Thread.Sleep(1);
                    }
                }
                while (!this.bUDPListenStop);
            }
            catch (Exception)
            {
            }
        }

        private void DealRuninfoPacketProc4RemoteOpenDoor()
        {
            try
            {
                do
                {
                    if (this.UDPQueue4RemoteOpenDoor.Count > 0)
                    {
                        byte[] buffer;
                        lock (this.UDPQueue4RemoteOpenDoor.SyncRoot)
                        {
                            buffer = (byte[]) this.UDPQueue4RemoteOpenDoor.Dequeue();
                        }
                        if ((buffer.Length == 0x40) && (buffer[1] == 0x40))
                        {
                            long num = this.getXidOfCommandShort(buffer);
                            int index = -1;
                            index = this.arrXIDSend.IndexOf(num);
                            if (index >= 0)
                            {
                                buffer[0] = 0x37;
                                long num3 = (long) this.arrXIDReceivedMobile[index];
                                IPEndPoint point = (IPEndPoint) this.arrXIDEndPointMobile[index];
                                Array.Copy(BitConverter.GetBytes(num3), 0, buffer, 40, 4);
                                lock (this.arrXIDSend.SyncRoot)
                                {
                                    this.arrXIDEndPointMobile.RemoveAt(index);
                                    this.arrXIDReceivedMobile.RemoveAt(index);
                                    this.arrXIDSend.RemoveAt(index);
                                }
                                this.UDP_OnlySend(buffer, 300, point.Address.ToString(), point.Port, 2);
                            }
                        }
                    }
                    else
                    {
                        Thread.Sleep(1);
                    }
                }
                while (!this.bUDPListenStop);
            }
            catch (Exception)
            {
            }
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing && (this.UdpSocket != null))
            {
                this.UdpSocket.Close();
            }
        }

        public wgControllerInfo GetControllerInfo(int controllerSN)
        {
            int index = arrSNReceived.IndexOf((uint) controllerSN);
            if (index >= 0)
            {
                return (arrControllerInfo[index] as wgControllerInfo);
            }
            return null;
        }

        private static int GetControllerType(int controllerSN)
        {
            if (controllerSN > 0x5f5e100)
            {
                if (controllerSN <= 0xbebc1ff)
                {
                    return 1;
                }
                if (controllerSN <= 0x11e1a2ff)
                {
                    return 2;
                }
                if (controllerSN <= 0x17d783ff)
                {
                    return 0;
                }
                if (controllerSN <= 0x1dcd64ff)
                {
                    return 4;
                }
            }
            return 0;
        }

        public byte[] getRecords()
        {
            if (this.receviedRecords.Count <= 0)
            {
                return null;
            }
            lock (this.receviedRecords.SyncRoot)
            {
                return (byte[]) this.receviedRecords.Dequeue();
            }
        }

        public byte[] getReply()
        {
            if (this.receviedCommandReply.Count <= 0)
            {
                return null;
            }
            lock (this.receviedCommandReply.SyncRoot)
            {
                return (byte[]) this.receviedCommandReply.Dequeue();
            }
        }

        public long getXid()
        {
            m_send_xid += 1L;
            if (m_send_xid >= 0x7fffffffL)
            {
                m_send_xid = 0x70000000L;
            }
            return m_send_xid;
        }

        public long getXidOfCommandShort(byte[] cmd)
        {
            long num = -1L;
            try
            {
                if (cmd.Length == 0x40)
                {
                    num = cmd[0x2b];
                    num = num << 0x18;
                    num += (cmd[40] | (cmd[0x29] << 8)) | (cmd[0x2a] << 0x10);
                }
            }
            catch (Exception)
            {
            }
            return num;
        }

        public bool IsWatching() => 
            this.bWatching;

        public int receivedCount() => 
            this.receviedRecords.Count;

        public void receivedReplyClear()
        {
            lock (this.receviedCommandReply.SyncRoot)
            {
                this.receviedCommandReply.Clear();
            }
        }

        public int receivedReplyCount() => 
            this.receviedCommandReply.Count;

        private void SockSpecialSet(ref Socket udpSock)
        {
            uint num = 0x80000000;
            uint num2 = 0x18000000;
            uint num3 = (num | num2) | 12;
            try
            {
                udpSock.IOControl((int) num3, new byte[] { Convert.ToByte(false) }, null);
            }
            catch (Exception)
            {
            }
        }

        public long UDP_OnlySend(byte[] cmd, long sequenceId)
        {
            long num = -13L;
            try
            {
                int num2 = 0x3e8;
                string str = "";
                int num3 = 0xea60;
                int num4 = num2;
                IPEndPoint endp = null;
                if (this.endp4broadcast == null)
                {
                    this.endp4broadcast = new IPEndPoint(IPAddress.Broadcast, 0xea60);
                }
                if (bUDPCloud > 0)
                {
                    string ip = str;
                    int port = num3;
                    UDPCloudGet(cmd, ref ip, ref port);
                    wgUdpComm.GetIPEndByIPAddr(ip, port, ref endp);
                    if (string.IsNullOrEmpty(ip))
                    {
                        return num;
                    }
                    num4 += wgUdpComm.timeourMsInternet;
                }
                if (endp == null)
                {
                    return num;
                }
                byte[] destinationArray = new byte[cmd.Length];
                Array.Copy(cmd, destinationArray, cmd.Length);
                long num6 = 0L;
                if ((cmd[2] == 0) && (cmd[3] == 0))
                {
                    if (((cmd[40] == 0) && (cmd[0x29] == 0)) && ((cmd[0x2a] == 0) && (cmd[0x2b] == 0)))
                    {
                        if (sequenceId == 0L)
                        {
                            sequenceId4SendCommand += 1L;
                            if ((sequenceId4SendCommand >= 0x4fffffffL) || (sequenceId4SendCommand < 0x40000001L))
                            {
                                sequenceId4SendCommand = 0x40000001L;
                            }
                            Array.Copy(BitConverter.GetBytes(sequenceId4SendCommand), 0, destinationArray, 40, 4);
                            num6 = sequenceId4SendCommand;
                            if (cmd.Length > 0x40)
                            {
                                int num7 = 0;
                                for (int i = 0; i < cmd.Length; i += 0x40)
                                {
                                    Array.Copy(BitConverter.GetBytes((long) (sequenceId4SendCommand + num7)), 0, destinationArray, i + 40, 4);
                                    num7++;
                                }
                            }
                        }
                        else
                        {
                            Array.Copy(BitConverter.GetBytes(sequenceId), 0, destinationArray, 40, 4);
                            num6 = sequenceId;
                            if (cmd.Length > 0x40)
                            {
                                int num9 = 0;
                                for (int j = 0; j < cmd.Length; j += 0x40)
                                {
                                    Array.Copy(BitConverter.GetBytes((long) (sequenceId + num9)), 0, destinationArray, j + 40, 4);
                                    num9++;
                                }
                            }
                        }
                    }
                    else
                    {
                        num6 = this.byteToLong(cmd, 40, 4);
                    }
                }
                EndPoint remoteEP = endp;
                try
                {
                    if (this.UdpSocket == null)
                    {
                        Thread.Sleep(20);
                    }
                    if (this.UdpSocket != null)
                    {
                        this.UdpSocket.SendTo(destinationArray, remoteEP);
                    }
                }
                catch (Exception)
                {
                    Thread.Sleep(20);
                    if (this.UdpSocket != null)
                    {
                        this.UdpSocket.SendTo(destinationArray, remoteEP);
                    }
                }
                num = num6;
            }
            catch (Exception)
            {
            }
            return num;
        }

        public int UDP_OnlySend(byte[] cmd, int parWaitMs, string ipAddr, int ipPort, int dest)
        {
            int num = -13;
            try
            {
                int num2 = parWaitMs;
                IPEndPoint endp = null;
                if (this.endp4broadcast == null)
                {
                    this.endp4broadcast = new IPEndPoint(IPAddress.Broadcast, 0xea60);
                }
                if (string.IsNullOrEmpty(ipAddr))
                {
                    endp = this.endp4broadcast;
                }
                else
                {
                    int num3 = -1;
                    int index = this.arrIPPORT.IndexOf($"{ipAddr}:{ipPort}");
                    if (index >= 0)
                    {
                        endp = (IPEndPoint) this.arrEndPoint[index];
                        num3 = (int) this.arrIGet[index];
                    }
                    else
                    {
                        num3 = wgUdpComm.GetIPEndByIPAddr(ipAddr, ipPort, ref endp);
                        if (num3 < 0)
                        {
                            return num;
                        }
                        this.arrIPPORT.Add($"{ipAddr}:{ipPort}");
                        this.arrEndPoint.Add(endp);
                        this.arrIGet.Add(num3);
                    }
                    if (num3 == 2)
                    {
                        num2 += wgUdpComm.timeourMsInternet;
                    }
                }
                if (endp == null)
                {
                    return num;
                }
                this.cmdtemp = new byte[cmd.Length];
                Array.Copy(cmd, this.cmdtemp, cmd.Length);
                EndPoint remoteEP = endp;
                try
                {
                    if (this.UdpSocket == null)
                    {
                        Thread.Sleep(20);
                    }
                    if (this.UdpSocket != null)
                    {
                        this.UdpSocket.SendTo(cmd, remoteEP);
                    }
                }
                catch (Exception)
                {
                    Thread.Sleep(20);
                    if (this.UdpSocket != null)
                    {
                        this.UdpSocket.SendTo(cmd, remoteEP);
                    }
                }
                num = 1;
            }
            catch (Exception)
            {
            }
            return num;
        }

        public static int UDPCloudGet(byte[] cmd, ref string ip, ref int port)
        {
            if (bUDPCloud == 1)
            {
                if (arrSNReceived.Count == 0)
                {
                    return 0;
                }
                uint num = 0;
                if ((cmd.Length % 0x40) == 0)
                {
                    num = BitConverter.ToUInt32(cmd, 4);
                }
                else
                {
                    num = BitConverter.ToUInt32(cmd, 12);
                }
                if (GetControllerType((int) num) != 0)
                {
                    int index = arrSNReceived.IndexOf(num);
                    if (index >= 0)
                    {
                        ip = (arrControllerInfo[index] as wgControllerInfo).IP;
                        port = (arrControllerInfo[index] as wgControllerInfo).PORT;
                        return 1;
                    }
                }
            }
            return 0;
        }

        private static void UDPCloudUpdate(uint sn, string ip, int port, byte[] recv)
        {
            if ((bUDPCloud == 1) && (GetControllerType((int) sn) != 0))
            {
                int index = arrSNReceived.IndexOf(sn);
                if (index < 0)
                {
                    arrSNReceived.Add(sn);
                    wgControllerInfo info = new wgControllerInfo();
                    info.update(sn, ip, port, DateTime.Now, BitConverter.ToString(recv));
                    arrControllerInfo.Add(info);
                }
                else if ((recv.Length % 0x40) == 0)
                {
                    (arrControllerInfo[index] as wgControllerInfo).update(sn, ip, port, DateTime.Now, BitConverter.ToString(recv));
                }
                else
                {
                    (arrControllerInfo[index] as wgControllerInfo).update(sn, ip, port, DateTime.Now);
                }
            }
        }

        private void UDPListenProc()
        {
            try
            {
                byte[] buffer2;
                IPEndPoint point = new IPEndPoint(IPAddress.Any, 0);
                IPEndPoint point2 = new IPEndPoint(IPAddress.Broadcast, 0xea60);
                byte[] buffer7 = new byte[12];
                buffer7[0] = 13;
                buffer7[1] = 13;
                byte[] buffer = buffer7;
                EndPoint remoteEP = point;
                EndPoint point4 = point2;
                this.UdpSocket.SendTo(buffer, point4);
                if (this.UDPQueue == null)
                {
                    this.UDPQueue = new System.Collections.Queue();
                }
                if (this.UDPQueue4Mobile == null)
                {
                    this.UDPQueue4Mobile = new System.Collections.Queue();
                }
                int length = 0;
            Label_0071:
                buffer2 = new byte[0x5dc];
                try
                {
                    length = this.UdpSocket.ReceiveFrom(buffer2, ref remoteEP);
                }
                catch (SocketException)
                {
                    this.UdpSocket = null;
                    this.UdpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    this.SockSpecialSet(ref this.UdpSocket);
                    this.UdpSocket.EnableBroadcast = true;
                    this.UdpSocket.ReceiveBufferSize = 0x1000000;
                    try
                    {
                        IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(this.serverIP), this.serverPort);
                        this.UdpSocket.Bind(localEP);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    goto Label_0302;
                }
                catch
                {
                    throw;
                }
                uint sn = 0;
                byte[] destinationArray = new byte[length];
                Array.Copy(buffer2, 0, destinationArray, 0, length);
                if (((destinationArray.Length == 0x40) && (destinationArray[0] == 0x37)) && (destinationArray[1] == 0x40))
                {
                    lock (this.UDPQueue4Mobile.SyncRoot)
                    {
                        this.UDPQueue4Mobile.Enqueue(destinationArray);
                        this.RemoteEndPointQueue4Mobile.Enqueue(remoteEP);
                        goto Label_0302;
                    }
                }
                if (((destinationArray.Length == 0x40) && (destinationArray[0] == 0x17)) && ((destinationArray[1] == 0x40) && ((destinationArray[0x2b] & 240) == 0x70)))
                {
                    lock (this.UDPQueue4RemoteOpenDoor.SyncRoot)
                    {
                        this.UDPQueue4RemoteOpenDoor.Enqueue(destinationArray);
                        this.RemoteEndPointQueue4RemoteOpenDoor.Enqueue(remoteEP);
                        goto Label_0302;
                    }
                }
                if ((destinationArray.Length % 0x40) == 0)
                {
                    if (((destinationArray[1] == 0x20) || (destinationArray[0] == 0x17)) && (((destinationArray[0x33] >= 0x13) || (destinationArray[8] != 0)) || (((destinationArray[9] != 0) || (destinationArray[10] != 0)) || (destinationArray[11] != 0))))
                    {
                        sn = BitConverter.ToUInt32(destinationArray, 4);
                    }
                }
                else
                {
                    sn = BitConverter.ToUInt32(destinationArray, 8);
                }
                if ((sn > 0x5f5e100) && (sn < 0x1dcd6500))
                {
                    UDPCloudUpdate(sn, ((IPEndPoint) remoteEP).Address.ToString(), ((IPEndPoint) remoteEP).Port, destinationArray);
                }
                if (this.bIncludeIP)
                {
                    byte[] buffer4 = new byte[length + 4];
                    Array.Copy(buffer2, 0, buffer4, 0, length);
                    try
                    {
                        Array.Copy((remoteEP as IPEndPoint).Address.GetAddressBytes(), 0, buffer4, length, 4);
                    }
                    catch
                    {
                    }
                    lock (this.UDPQueue.SyncRoot)
                    {
                        this.UDPQueue.Enqueue(buffer4);
                        goto Label_0302;
                    }
                }
                byte[] buffer6 = new byte[length];
                Array.Copy(buffer2, 0, buffer6, 0, length);
                lock (this.UDPQueue.SyncRoot)
                {
                    this.UDPQueue.Enqueue(buffer6);
                }
            Label_0302:
                if (!this.bUDPListenStop)
                {
                    goto Label_0071;
                }
            }
            catch (Exception)
            {
            }
        }

        public bool IncludeIPInfo
        {
            get => 
                this.bIncludeIP;
            set
            {
                this.bIncludeIP = value;
            }
        }
    }
}

